//
//  OpenAWebView.m
//  hengqintong
//
//  Created by DongWei on 2019/11/11.
//  Copyright © 2019 Facebook. All rights reserved.
//

#import "iOSModule.h"
#import "ToWebView.h"
#import "NSData+JSONTool.h"
#import "IWCScanBarCodeViewController.h"
#import <React/RCTLog.h>

@interface iOSModule(){
  Boolean recognizerStarted;
  RCTResponseSenderBlock _alertCallback;
}

@end

@implementation iOSModule

RCT_EXPORT_MODULE(); // 将改 NSObject 类导出为 RN 模组

// 导出一个 sendLocalNotification 方法给 RN,用于RN 接收到 iOS 推送后本地弹出推送
RCT_EXPORT_METHOD(sendLocalNotification: (NSString *)title andMsg: (NSString *)message){
  //  [self requestNotification];
  
  //1. 创建本地通知
  UILocalNotification *localNote = [[UILocalNotification alloc] init];
  
  //2. 设置本地通知的内容
  //2.1 设置通知发出的时间
  localNote.fireDate = [NSDate dateWithTimeIntervalSinceNow:1];
  //2.2设置通知的内容
  localNote.alertBody = message;
  //2.3 设置解屏滑块下Button的文字
  localNote.alertAction = message;
  //2.4决定alertAction是否生效
  localNote.hasAction = YES;
  //2.5设置通知中心的标题
  localNote.alertTitle = title;
  //2.6设置通知音效
  localNote.soundName = UILocalNotificationDefaultSoundName;
  //2.7设置应有程序图标右上角的提示数字
  localNote.applicationIconBadgeNumber = 1;
  
  //3.调度通知
  [[NSOperationQueue mainQueue] addOperationWithBlock:^{//只留下主线程返回的进度数据
    [[UIApplication sharedApplication] presentLocalNotificationNow:localNote];
  }];
  NSLog(@"发送通知");
}

// 导出一个 toWebView 方法给 RN,参数为 NSString 类型,用于打开 WKWebView 并传输 URL
RCT_EXPORT_METHOD(toWebView:(NSString *)url withName:(NSString *)name withCloseText:(NSString *)close)
{
  ToWebView *toWebView = [[ToWebView alloc]init];
  toWebView.url = url;
  toWebView.name = name;
  toWebView.close = close;
  
  [[NSOperationQueue mainQueue] addOperationWithBlock:^{//只留下主线程返回的进度数据
    UIViewController *rootViewController = [[[UIApplication sharedApplication] keyWindow] rootViewController];
    
    // 在这里加一个这个样式的循环
    while (rootViewController.presentedViewController)
    {
      // 这里固定写法
      rootViewController = rootViewController.presentedViewController;
    }
    // 然后再进行present操作
    [rootViewController presentViewController:toWebView animated:YES completion:nil];
  }];
}

RCT_EXPORT_METHOD(openQR:(NSString *)simple){
  __weak __typeof__(self) weakSelf = self;
  _QRVC = [[IWCScanBarCodeViewController alloc]init];
  _QRVC.IWCQRCodeSuccessBlock = ^(IWCScanBarCodeViewController *ScanVC, NSString *resultStr){
    [weakSelf startCameraScanCallback:resultStr andVC:ScanVC];
  };
  
  [[NSOperationQueue mainQueue] addOperationWithBlock:^{//只留下主线程返回的进度数据
    UIViewController *rootViewController = [[[UIApplication sharedApplication] keyWindow] rootViewController];
    
    // 在这里加一个这个样式的循环
    while (rootViewController.presentedViewController)
    {
      // 这里固定写法
      rootViewController = rootViewController.presentedViewController;
    }
    // 然后再进行present操作
    [rootViewController presentViewController:self.QRVC animated:YES completion:nil];
  }];
}


// OC 调用 JS 方法
-(NSArray *)supportedEvents{
  return@[@"onCompleted", @"ScanCallback"];
}
//
//- (void)startObserving{
//  [[NSNotificationCenter defaultCenter] addObserver:self
//                                           selector:@selector(OnRecognizedCompleted:result:statusCode:errorMessage:)
//                                               name:@"onCompleted"
//                                             object:nil];
//  [[NSNotificationCenter defaultCenter] addObserver:self
//                                           selector:@selector(startCameraScanCallback:andVC:)
//                                               name:@"ScanCallback"
//                                             object:nil];
//}

-(void)startCameraScanCallback:(NSString *)callback andVC:(IWCScanBarCodeViewController *)scanVC{
  NSDictionary *callbackDic = @{@"data":callback};
  NSData *jsonData = [NSJSONSerialization dataWithJSONObject:callbackDic options:0 error:0];
  id jsonObject = [NSData objectFromJSONData:jsonData];
  
  [self sendEventWithName:@"ScanCallback" body:(id)jsonObject];
  
  [scanVC dismissViewControllerAnimated:YES completion:nil];
}

/**
 *5. 录音相关回调
 */
- (void)recorderDidStart {
  NSLog(@"Did start recorder!");
}

- (void)recorderDidStop {
  NSLog(@"Did stop recorder!");
}

- (void)voiceDidFail:(NSError *)error {
  NSLog(@"Did recorder error!");
}


- (void)voiceVolume:(NSInteger)volume {
  // onVoiceVolume if you need.
}


@end
